﻿namespace WMS6.Web.Billing
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;

    public class BillingService : BasePage
    {
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected HtmlForm F9161;
        protected HtmlInputHidden hdnBillingServiceID;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnLineID;
        protected HtmlHead Head1;
        protected Label lblBillingServiceID;
        protected Label lblDesrc;
        protected Label lblMsg;
        protected Label lblOwner;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtBillingServiceID;
        protected TextBox txtDesrc;
        protected UpdatePanel up1;

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtBillingServiceID.Text = "";
            this.txtBillingServiceID.Enabled = true;
            base.DataEntityViewState = null;
            this.hdnFlag.Value = "";
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtBillingServiceID.Text.Trim()))
            {
                try
                {
                    List<string> BillingServiceIDS = new List<string> {
                        this.txtBillingServiceID.Text.Trim()
                    };
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(BillingServiceIDS, "BILLING_SERVICE") + base.GetGlobalMsgResource("F9160_006");
                    base.DataEntityViewState = null;
                    this.btnCopy.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnSave.Enabled = false;
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9160_007") + ex.Message;
                }
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("BillingServiceQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("BillingServiceList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            base.Server.Transfer(string.Format("BillingService.aspx?from={0}", base.UrlEncode(this.hdnFrom.Value.Trim())), false);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.hdnFlag.Value == "1")
            {
                this.UpdateBillingServiceInfo();
            }
            else
            {
                this.InsertBillingServiceInfo();
            }
        }

        private void InsertBillingServiceInfo()
        {
            try
            {
                BillingServiceInfo billingServiceInfo = new BillingServiceInfo {
                    BillingServiceID = this.txtBillingServiceID.Text,
                    Descr = this.txtDesrc.Text,
                    IsActive = this.chkIsActive.Checked ? "Y" : "N",
                    CreatedBy = base.UserInfo.UserID
                };
                this.hdnBillingServiceID.Value = BaseInfoServiceClientProxy.GetInstance().Insert(billingServiceInfo, "BILLING_SERVICE");
                this.hdnLineID.Value = "10";
                this.hdnFlag.Value = "1";
                base.DataEntityViewState = billingServiceInfo;
                this.txtBillingServiceID.Text = this.hdnBillingServiceID.Value;
                this.txtBillingServiceID.Enabled = false;
                this.lblMsg.Text = this.hdnBillingServiceID.Value + base.GetGlobalMsgResource("F9160_004");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9160_005") + ex.Message;
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnFlag.Value = "1";
                this.hdnBillingServiceID.Value = base.Request.QueryString["ID"].ToString();
                this.SetBillingServiceInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["detail"]))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,2) </script>", false);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetBillingServiceInfo2Form()
        {
            try
            {
                BillingServiceInfo billingServiceInfo = (BillingServiceInfo) BaseInfoServiceClientProxy.GetInstance().GetInfoByID(this.hdnBillingServiceID.Value, "BILLING_SERVICE");
                if (billingServiceInfo != null)
                {
                    this.txtBillingServiceID.Text = billingServiceInfo.BillingServiceID;
                    this.txtBillingServiceID.Enabled = false;
                    this.txtDesrc.Text = billingServiceInfo.Descr;
                    this.chkIsActive.Checked = billingServiceInfo.IsActive == "Y";
                    base.DataEntityViewState = billingServiceInfo;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateBillingServiceInfo()
        {
            try
            {
                BillingServiceInfo billingServiceInfo = base.DataEntityViewState as BillingServiceInfo;
                billingServiceInfo.Descr = this.txtDesrc.Text;
                billingServiceInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                billingServiceInfo.UpdatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(billingServiceInfo, "BILLING_SERVICE") + base.GetGlobalMsgResource("F9160_002");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9160_003") + ex.Message;
            }
        }
    }
}

